import axios from "axios";
import { showToast } from "vant";
import { getItem } from "./storage";

const service = axios.create({
    // 设置超时时间
    timeout: 60000,
    baseURL: import.meta.env.ENV_BASE_URL,
});

service.interceptors.request.use(
    (config) => {
        if (import.meta.env.DEV) {
            // config.headers["Operator"] = "1301080";
            config.headers["Authorization"] =
                "hc6N+A1VTqsEu1epS/B7YdaM7nvz+c6ARO1u3ZxQWEcEOPs5bCopTJN5joULVl3UWvLHRePnASA1QR8DpfFo+Q==";
            config.headers["App-Version"] = "1.0.0";
            config.headers["App-Channel"] = 1;
        } else {
            config.headers["Authorization"] = getItem("Authorization");
            config.headers["App-Version"] = getItem("AppVersion");
            config.headers["App-Channel"] = getItem("AppChannel");
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

/**
 * 请求响应拦截
 * 用于处理需要在请求返回后的操作
 */
const success_toast_code = [] as number[];
const err_notoast_code = [] as number[];
const err_userdefined_code = {
    '401': "登录过期",
};
enum Gender {
    '登录过期' = 401
};
service.interceptors.response.use(
    (res) => {
        if (res.status == 200) {
            const code: number = res.data.code;
            //成功接口，不弹提示
            if (code == 0) {
                return Promise.resolve(res.data.data);
                //实名认证身份证已经在别的账号认证过
            } else if (res.data.code === 540010) {
                return Promise.reject(res.data);
            }
            //成功接口，弹后端提示
            else if (success_toast_code.includes(code)) {
                showToast(res.data.message);
                return Promise.resolve(res.data.data);
            }
            //失败接口，不弹提示
            else if (err_notoast_code.includes(code)) {
                return Promise.reject(res.data);
            }
            //失败接口，弹前端的提示
            else if (code in err_userdefined_code) {
                showToast(Gender[code]);
                return Promise.reject(res.data);
            }
            //默认失败，弹后端提示
            showToast(res.data.message);
            return Promise.reject(res.data);
        }
        return Promise.resolve(res.data);
    },
    (err) => {
        if (err && err.res) {
            switch (err.res.status) {
                case 400:
                    err.message = "请求出错";
                    break;
                case 401:
                    err.message = "授权失败,请重新登录";
                    break;
                case 403:
                    err.message = "拒绝访问";
                    break;
                case 404:
                    err.message = "请求错误,未找到该资源";
                    break;
                case 500:
                    err.message = "服务器端出错";
                    break;
            }
        } else {
            err.message = "连接服务器失败";
        }
        showToast(err.message);
        return Promise.reject(err);
    }
);
service.defaults.headers.post["Content-Type"] =
    "application/json;charset=UTF-8";

export const get = (url: string, params?: any): any => service.get(url, { params });
export const post = (url: string, data?: any): any => service.post(url, data);
